package day0803;

import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//多线程
//https://www.nowcoder.com/practice/cd99fbc6154d4074b4da0e74224a1582?tpId=37&&tqId=21272&rp=1&ru=/ta/huawei&qru=/ta/huawei/question-ranking
public class DuoXianCheng {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Semaphore semaphore = new Semaphore(1);
        try {
            while(sc.hasNext()){
                int n =sc.nextInt();
                semaphore.acquire();
                Resource resource = new Resource();
                new Thread(()->{
                    for (int i = 0; i <n; i++) {
                        resource.printA();
                    }
                },"1").start();

                new Thread(()->{
                    for (int i = 0; i <n; i++) {
                        resource.printB();
                    }
                },"2").start();

                new Thread(()->{
                    for (int i = 0; i <n; i++) {
                        resource.printC();
                    }
                },"3").start();

                new Thread(()->{
                    for (int i = 0; i <n; i++) {
                        resource.printD();
                    }
                    System.out.println("");
                    semaphore.release();
                },"4").start();
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Resource{

    private int num = 1;
    private Lock lock = new ReentrantLock();
    private Condition  condition1 = lock.newCondition();
    private Condition  condition2 = lock.newCondition();
    private Condition  condition3 = lock.newCondition();
    private Condition  condition4 = lock.newCondition();


    public void printA(){
        lock.lock();
        try {
            while(num!=1){
                condition1.await();
            }
            System.out.print("A");
            num=2;
            condition2.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void printB(){
        lock.lock();
        try {
            while(num!=2){
                condition2.await();
            }
            System.out.print("B");
            num=3;
            condition3.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void printC(){
        lock.lock();
        try {
            while(num!=3){
                condition3.await();
            }
            System.out.print("C");
            num=4;
            condition4.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void printD(){
        lock.lock();
        try {
            while(num!=4){
                condition4.await();
            }
            System.out.print("D");
            num=1;
            condition1.signal();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}

